package com.fuzhenguo.boltmusic;

import java.io.File;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Audio.Albums;
import android.provider.MediaStore.Audio.Media;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;

public class Album {
    private static final String TAG = "Album";
    private Uri mUri;
    private Uri mAlbumArtUri;
    private String mAlbumKey;
    private String mAlbumName;
    private int mId;
    private ContentResolver mContentResolver;
    private static Context mContext;

    /**
     * @param albumKey
     */
    public Album(String albumKey) {
        mContentResolver = mContext.getContentResolver();
        Uri uri = Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = mContentResolver.query(uri, null, Media.ALBUM_KEY
                + " = ?", new String[] { albumKey }, null);

        if (cursor == null) {
            Log.e(TAG, "Query failed.");
        } else if (!cursor.moveToFirst()) {
            Log.e(TAG,
                    "There is no accordant album on the external storage mapping to the albumKey :"
                            + albumKey + ".");
        } else {
            int albumNameColumnId = cursor.getColumnIndex(Albums.ALBUM);
            int albumIdColumnId = cursor.getColumnIndex(Albums.ALBUM_ID);
            mAlbumName = cursor.getString(albumNameColumnId);
            mId = cursor.getInt(albumIdColumnId);
            mUri = Albums.getContentUri(mAlbumName);
            mAlbumKey = albumKey;
        }
    }

    /**
     * Returns whether this album is valid one.
     * 
     * @category test
     * @return Whether this album is valid one.
     */
    public boolean isValid() {
        if (mContentResolver == null) {
            mContentResolver = mContext.getContentResolver();
        }
        Uri uri = Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = mContentResolver.query(uri, null, Media.ALBUM_KEY
                + " = ?", new String[] { mAlbumKey }, null);

        if (cursor == null) {
            Log.e(TAG, "Query failed.");
        } else if (!cursor.moveToFirst()) {
            Log.e(TAG, "There is no accordant album on the external storage.");
            return false;
        } else {
            //TODO add file check for Uri
//            File tempFile = new File(mUri.getPath());
//            if (tempFile.exists()) {
//                return true;
//            } else {
//                return false;
//            }

            return true;
        }
        return false;
    }

}
